"use strict";
require("log-timestamp");
const fs = require('fs');
const express = require("express");
const router = express.Router();
const deviceModel = require("../models/powermodel");
const secret = require('../config').secret
const _ = require("lodash");
const modelUtils = require("../utils/modelUtils");
const asyncMethod = require("async");
const assert = require("assert");
const debug = true;
const isTest = true;

router.get("/", (req, res) => {
  let deviceId = req.query.deviceid;
  let rawdata = fs.readFileSync(__dirname + '/mockdata.json');
  let powerData = JSON.parse(rawdata);
  if(deviceId){ //get all the data
    powerData = [{
      "deviceId": "1234567890-1",
      "device": "Huawei OLT model",
      "voltage": "134",
      "current": "2.4",
      "runTime": "13453",
      "consumption": "33",
      "temperature": "45",
      "type": "olt"
    }]
  }
  return res.json({
    errorCode: 0, data: powerData
  });
})

/**
 * @apiVersion 0.1.0
 * @ApiDescription devicepower  get device power status
 * @api{post} /api/follows/followinglist 获取我关注的人列表
 * @apiName GetPower
 * @apiGroup Power
 *
 * @apiParam token 发起人token, will use the secret as per in the config file
 * @apiParam {String} olt deviceId
 * @apiParam {String} timestamp 只取>该时间戳的列表 0 == current
 * @apiSuccessExample 成功例子:

 * @apiSuccess {Number} errorCode 返回代码 0 ok, other code: error
 * @apiSuccess {String} message 返回信息
 */
router.post("/", (req, res) => {
  let deviceId = req.body.deviceId
  let timestamp = req.body.timestamp ? req.body.timestamp : 0,
    token = req.body.token;
  if(!deviceId)
    res.json({errorCode: 1, message: "err: no deviceId"});
  if(!isTest || token !== secret){
    res.json({errorCode: 2, message: "err: incorrect token"});
  }
  if(debug) console.log("deviceId:::", deviceId);

  if(timestamp == 0){
    getPower(deviceId);
  }else{
    getPower_graph(deviceId, timestamp);
  }


  function getPower_graph(deviceId, timestamp){
    return;
  }

  /**
   * get current power status from database
   * @param {*} deviceId
   */
  function getPower(deviceId){
    if(isTest){
      let mockdata = [{
        deviceId: "1234567890",
        device: "Huawei OLT model",
        voltage: "134",
        current: "2.4",
        runTime: "13453",
        consumption: "33",
        temperature: "45",
        deviceType: "olt"
      },
        {
          deviceId: "1234567890-1",
          device: "Huawei OLT model",
          voltage: "134",
          current: "2.4",
          runTime: "13453",
          consumption: "33",
          temperature: "45",
          deviceType: "olt"
        }
      ]
      return res.json({
        errorCode: 0,
        message: "the following list is empty",
        data: mockdata,
      })
    }
  }
})

module.exports = router;
